module GameUtil {
    /**对象池 ——刘伟*/

    export class GamePools {
        private constructor() {
        }
        /**对象池单例 */
        public static instance: GamePools;

        /**飞机对象池存储字典 */
        private flyDict: Object = {};
        /**子弹对象池存储字典 */
        private bulletDict:Object = {};

        /**对象池构造方法 */
        public static creatGamePools(): GamePools {
            if (GamePools.instance == null) {
                GamePools.instance = new GamePools();
            }
            return GamePools.instance;
        }

        /**创建飞机，注意在创建后给飞机贴图，添加飞机和血条至舞台*/
        public producePlane(type): Fighter.BasicFly {
            if (this.flyDict[type] == null) {
                this.flyDict[type] = [];
            }
            let dict: Fighter.BasicFly[] = this.flyDict[type];
            let fly: Fighter.BasicFly;
            if (dict.length > 0) {
                //console.log(type+"型"+"飞机数目："+dict.length);
                fly = dict.pop();  
            }
            else {
                switch (type) {
                    case 0:
                        fly = new Fighter.BasicFly();
                        break;
                    case 1:
                        fly = new Fighter.PlaneAddSpeed();
                        break;
                    case 2:
                        fly = new Fighter.PlaneSelfDestruction();
                        break;
                    case 3:
                        fly = new Fighter.PlaneShield();
                        break;
                    case 4:
                        fly = new Fighter.PlaneShot();
                        break;
                    default:
                        console.error("不存在" + type + "型飞机");
                        break;
                }
            }
            return fly;
        }
        /**回收飞机*/
        public reclaim(fly: Fighter.BasicFly) {
            let dict: Fighter.BasicFly[] = this.flyDict[fly.type];
            if (dict.indexOf(fly) == -1) {
                dict.push(fly);
            }
        }

        /**创建子弹*/
        public produceBlt(blt_type): Bullet.BasicBullet {

            if (this.bulletDict[blt_type] == null) {
                this.bulletDict[blt_type] = [];
            }
            let dict: Bullet.BasicBullet[] = this.bulletDict[blt_type];
            let bullet: Bullet.BasicBullet;;
            if (dict.length > 0) {
                //console.log(blt_type+"型"+"子弹池子弹数目："+dict.length);
                bullet = dict.pop();
            }
            else {
                switch (blt_type) {
                    case 0:
                        bullet = new Bullet.BasicBullet();
                        break;
                    case 1:
                        bullet = new Bullet.BulletBattery();
                        break;
                    case 2:
                        bullet = new Bullet.BulletEnemy();
                        break;
                    default:
                        console.error("不存在" + blt_type + "型号子弹");
                        break;
                }
            }
            return bullet;
        }

        /**回收子弹*/
        public reclaimBlt(bullet: Bullet.BasicBullet) {
            let dict: Bullet.BasicBullet[] = this.bulletDict[bullet.bulletType];
            if (dict.indexOf(bullet) == -1) {
                dict.push(bullet);
            }
        }


    }
}